
/* converts from our image format to prodos's BIN format */

#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>

char far *screenbuffer;

#define S_IWRITE    0000200     /* write permission, owner   */

int makefile(char *filename)
{
int fh;

if((fh = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IWRITE)) == -1)
        return 0;
    write(fh,(char *)&screenbuffer[0],0x2000);
    if(close(fh) == -1)
    return 0;
}


main(int argc, char *argv[])
{
    FILE *picture;
    char buf[66],buf2[66];
    char *wordptr;

    if(argc==2)
    {
        screenbuffer = _fmalloc(0x2000);
        memset(screenbuffer,0,0x2000);
        strcpy(buf,argv[1]);
        wordptr=strtok(buf,".");
        sprintf(buf2,"%s.BIN",buf);
        piclode(argv[1]);
        makefile(buf2);
        _ffree(screenbuffer);
      }
    exit(0);
}


piclode(name)
char *name;
{
   int fh,y,temp,packet=40,bos;
   char buffer[2];

   fh = open(name,O_RDONLY|O_BINARY); /* open a binary file */


   read(fh,buffer,2);

   for(y=0;y<191;y++)
   {
     gethibase(y,&temp);
     read(fh,(char *)&screenbuffer[temp],packet);
     /* read each raster to the screenbuffer */
     }
   close(fh);

}

/* provides base offset for hires scanlines         */
/* does not bother to check whether we are in range */
/* gets the address as quickly as possible          */
/* stays away from processor intensive mul and div  */

gethibase(currentline,currentbase)
int currentline;
int *currentbase;
{
  int ybase=0,z,a;

	if(currentline >63)
	   {
	   if (currentline < 128)
	       {
		ybase+=0x28;
		currentline-=64;
	       }
	   else
	       {
	       ybase+=0x50;
	       currentline-=128;
	       }
	    }

	    z=(currentline>>3);
	    a = (z<<7)|ybase;
	    *currentbase = (currentline - (z<<3))<<10 | a;
}


